package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.knowman.ObService.OBKnowCategoryService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ReturnMessage.MExKnowMain9_2_SN_KCN;
import com.example.knowman.ReturnMessage.Message;
import com.example.knowman.dao.*;
import com.example.knowman.entity.knowCategory;
import com.example.knowman.entity.knowMain;
import com.example.knowman.service.IKnowCategoryService;
import com.example.knowman.service.IKnowMainService;
import com.example.knowman.service.IStaffListService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

//9.2发布知识
@RestController
@RequestMapping("/drive")
public class DriveController {
    private final OBKnowMainService obKnowMainService;

    private final OBKnowCategoryService obKnowCategoryService;

    private final IStaffListService iStaffListService;

    public DriveController(OBKnowMainService obKnowMainService,OBKnowCategoryService obKnowCategoryService,IStaffListService iStaffListService) {
        this.obKnowMainService = obKnowMainService;
        this.obKnowCategoryService = obKnowCategoryService;
        this.iStaffListService = iStaffListService;
    }


    //2.中间总结数据：今年累计知识提交数、今年累计知识通过数、今日累计知识通过数
    @GetMapping("getMiddleConclusion")
    public ResponseEntity<DriveMiddleConclusion> getMiddleConclusion(){
        //今年累积知识提交数，应该就是createTime是今年的
        //今年累计知识通过数，createTime是今年的+状态=3
        List<knowMain> Year_KML = obKnowMainService.getYear();
//        System.out.println(KML);
        Integer year_pass = 0;
        Integer year_commit = Year_KML.size();
        for(knowMain item:Year_KML){
            if(item.getKnowState()==3){
                year_pass++;
            }
        }


        //今日累计知识通过数，createTime是今天的+状态=3
        List<knowMain> KML = obKnowMainService.getToday();
//        System.out.println(KML);
        Integer day_pass = 0;
        Integer all = KML.size();
        for(knowMain item:KML){
            if(item.getKnowState()==3){
                day_pass++;
            }
        }

        DriveMiddleConclusion res = new DriveMiddleConclusion();
        res.setDay_pass(day_pass);
        res.setYear_commit(year_commit);
        res.setYear_pass(year_pass);

        return ResponseEntity.ok(res);
    }


    //3.知识分享排行榜
    @GetMapping("getRanks")
    public ResponseEntity<RankingDrive> getRanks() {
        List<knowCategory> KCL = obKnowCategoryService.getAll();
        List<knowMain> KML = obKnowMainService.getAll();

        //用Map表存<KnowCateId，对应knowMain的知识数量>
        Map<Integer,Integer> nums = new HashMap<>();
        for (knowCategory item:KCL){
            Integer sum=0;
            for(knowMain KMitem:KML){
                if (KMitem.getKnowCateId()==item.getKnowCateId()){
                    sum++;
                }
            }
            nums.put(item.getKnowCateId(),sum);
        }

//        System.out.println(nums);

        RankingDrive res = new RankingDrive();
        List<RankingDriveItem> data=new ArrayList<>();

        //用Map表依次存三级、二级、一级的个数
//        Map<Integer,Integer> sums = new HashMap<>();
        for(knowCategory item:KCL){
            if(item.getCateId().length() == 6){
                nums.put(item.getParentId(), nums.get(item.getParentId())+nums.get(item.getKnowCateId()));
            }
        }
        for(knowCategory item:KCL){
            if(item.getCateId().length() == 4){
                nums.put(item.getParentId(), nums.get(item.getParentId())+nums.get(item.getKnowCateId()));
            }
        }

        //获得所有大类
        for(knowCategory item:KCL){
            if(item.getParentId() == 0){
                RankingDriveItem temp =new RankingDriveItem();
                temp.setName(item.getKnowCateName());

                //统计
                temp.setValue(nums.get(item.getKnowCateId()));
                data.add(temp);
            }
        }

//        System.out.println(data);

        res.setData(data);
        return ResponseEntity.ok(res);

    }

    //4.今日知识通过率
    @GetMapping("KnowMainPass")
    public ResponseEntity<String> KnowMainPass() {
        List<knowMain> KML = obKnowMainService.getToday();
//        System.out.println(KML);
        Integer pass = 0;
        Integer all = KML.size();
        for(knowMain item:KML){
            if(item.getKnowState()==3){
                pass++;
            }
        }
        double result = (double) pass / all * 100;
        int percentage = (int) result;
        return ResponseEntity.ok(percentage + "%");

    }

    //6.最热知识排行榜
    @GetMapping("TopKnowMain")
    public ResponseEntity<List<DriveTopKnowMain>> TopKnowMain() {
        List<knowMain> KML = obKnowMainService.Top();
//
//        System.out.println(KML);
        Map<String, String> SL= iStaffListService.getIdAndName();
        List<DriveTopKnowMain> res = new ArrayList<>();

        for(knowMain item:KML){
            DriveTopKnowMain temp = new DriveTopKnowMain();
            temp.setKnowId(item.getKnowId());
            temp.setReadCount(item.getReadCount());
            temp.setKnowTitle(item.getKnowTitle());
            temp.setStaffName(SL.get(item.getStaffCode()));

            res.add(temp);
        }
        return ResponseEntity.ok(res);
    }
//

    //8.每日知识发布数据统计图
    @GetMapping("KnowMainCreate")
    public ResponseEntity<List<DriveCountDay>> KnowMainCreate() {
        List<DriveCountDay> res = new ArrayList<>();


        List<knowMain> KML = obKnowMainService.getAll();

        Map<LocalDate, Long> countByDate = KML.stream()
                .collect(Collectors.groupingBy(knowMain -> {
                    LocalDateTime localDateTime = LocalDateTime.ofInstant(knowMain.getCreatetime().toInstant(), ZoneId.systemDefault());
                    return localDateTime.toLocalDate();
                }, Collectors.counting()));

        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd");
        for (int i = 0; i < 7; i++) {
            DriveCountDay temp = new DriveCountDay();

            LocalDate date = today.minusDays(i);
            Long count = countByDate.get(date);
            if (count == null) {
                count = 0L;
            }

            String dateString = date.format(formatter);
            temp.setCount(count);
            temp.setDate(dateString);
            res.add(temp);
//            System.out.printf("%d天前有%d个knowMain对象被创建\n", i, count);
        }
        
        //逆序
        Collections.reverse(res);


        return ResponseEntity.ok(res);

    }
//

}
